﻿using System;
using System.Collections.Generic;
using System.Text;

namespace EF.Util
{
    public class SqlParameterInfo
    {
        public SqlParameterInfo() { }
        public SqlParameterInfo(string name,string value)
        {
            this.Name = name;
            this.Value = value;
        }
   
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _value;

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }          
    }

    public abstract class SqlParameterHelp
    {
        public static string ConvertToSqlParameter(List<SqlParameterInfo> splist)
        {
            StringBuilder Sb = new StringBuilder();
            foreach (SqlParameterInfo sp in splist)
            {
                if (sp.Value != "")
                {
                    Sb.Append("@" + sp.Name + "='" + sp.Value + "',");
                }
            }

            return Sb.ToString().Substring(0, Sb.ToString().Length - 1);
        }

        public static List<SqlParameterInfo> AddToParameterList(List<SqlParameterInfo> splist, string name, string value)
        {
            if (splist != null)
            {
                splist.Add(new SqlParameterInfo(name, value));
            }
            else
            {
                splist = new List<SqlParameterInfo>();
                splist.Add(new SqlParameterInfo(name, value));
            }

            return splist;
        }

        public static bool Exists(List<SqlParameterInfo> splist, string name)
        {
            bool b = false;
            foreach (SqlParameterInfo sp in splist)
            {
                if (sp.Name == name)
                {
                    b = true;
                }
            }

            return b;
        }

        public static string GetValueByName(List<SqlParameterInfo> splist, string name)
        {
            string s = "";
            foreach (SqlParameterInfo sp in splist)
            {
                if (sp.Name == name)
                {
                    s = sp.Value;
                }
            }

            return s;
        }

        public static List<SqlParameterInfo> SetValueByName(List<SqlParameterInfo> splist, string name, string value)
        {
            List<SqlParameterInfo> list = new List<SqlParameterInfo>();
            foreach (SqlParameterInfo sp in splist)
            {
                if (sp.Name == name)
                {
                    sp.Value = value;
                }

                list.Add(sp);
            }

            return list;
        }
    }
}
